package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Department;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.query.EmployeeQueryObject;
import cn.wolfcode.crm.serivce.IEmployeeService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.JSONUtil;
import cn.wolfcode.crm.util.PageResult;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService service;

    @RequestMapping("view")
    @RequiresPermissions(value={"employee:view","员工界面"},logical = Logical.OR)
    public String view() throws Exception {
        return "employee";
    }

    @RequestMapping("query")
    @ResponseBody
    public PageResult query(EmployeeQueryObject qo) throws Exception {
        PageResult query = service.query(qo);
        return query;
    }
    //验证用户名的方法
    @RequestMapping("validate")
    @ResponseBody
    public Boolean validate(String username,Long id) throws Exception {
        Boolean aBoolean = service.selectByUsername(username,id);
        return aBoolean;
    }
    //验证用户名的方法
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value={"employee:saveOrUpdate","员工新增/编辑"},logical = Logical.OR)
    public JSONResult saveOrUpdate(Employee emp) throws Exception {
        try {
            service.saveOrUpdate(emp);
            return new JSONResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult().mark("保存失败!");
        }
    }
    @RequestMapping("updatePassword")
    @ResponseBody
    @RequiresPermissions(value={"employee:updatePassword","员工密码修改"},logical = Logical.OR)
    public JSONResult updatePassword(Employee emp) throws Exception {
        try {
            service.updatePassword(emp);
            return new JSONResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult().mark("密码保存失败!");
        }
    }
    //操作员工离职复职
    @RequestMapping("changeState")
    @ResponseBody
    @RequiresPermissions(value={"employee:changeState","员工状态修改"},logical = Logical.OR)
    public JSONResult changeState(Long id) throws Exception {
        try {
            service.changeState(id);
            return new JSONResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult().mark("操作失败!");
        }
    }
    @RequestMapping("exportXls")
    public void exportXls(HttpServletResponse response) throws Exception {
        //设置下载响应头
        response.setHeader("Content-Disposition","attachment;filename=employee.xls");
        //创建excel文件
        Workbook wb = new HSSFWorkbook();
        //创建工作薄
        Sheet sheet = wb.createSheet("day01");
        //查询员工数据
        List<Employee> employees = service.selectAll();
        //创建表格第一行
        Row row = sheet.createRow(0);
        //设置标题
        row.createCell(0).setCellValue("用户名");
        row.createCell(1).setCellValue("真实姓名");
        row.createCell(2).setCellValue("电话");
        row.createCell(3).setCellValue("邮箱");
        row.createCell(4).setCellValue("部门");
        row.createCell(5).setCellValue("入职时间");
        row.createCell(6).setCellValue("在职状态");
        row.createCell(7).setCellValue("超级管理员");
        //设置第二行开始
        for(int i=0;i< employees.size();i++){
            //从第二行开始创建行
            Row row1 = sheet.createRow(i + 1);
            //从列表的第一开始取出所有的员工对象
            Employee employee = employees.get(i);
            //创建单元格
            row1.createCell(0).setCellValue(employee.getUsername());
            row1.createCell(1).setCellValue(employee.getRealname());
            row1.createCell(2).setCellValue(employee.getTel());
            row1.createCell(3).setCellValue(employee.getEmail());
            row1.createCell(4).setCellValue(employee.getDept().getName());
            row1.createCell(5).setCellValue(employee.getHireDate());
            row1.createCell(6).setCellValue(employee.getState());
            row1.createCell(7).setCellValue(employee.getAdmin());
        }
        //输出到浏览器
        wb.write(response.getOutputStream());
    }
    @RequestMapping("importXls")
    public void importXls(MultipartFile file) throws Exception {
        //获取excel文件
        Workbook wb = new HSSFWorkbook(file.getInputStream());
        //获取工作簿
        Sheet sheet = wb.getSheet("day01");
        //获取总行数
        int lastRowNum = sheet.getLastRowNum();
        for(int i=1;i<=lastRowNum;i++){
            Row row = sheet.getRow(i);
            Employee employee = new Employee();
            employee.setUsername(row.getCell(0).getStringCellValue());
            employee.setRealname(row.getCell(1).getStringCellValue());
            employee.setTel(row.getCell(2).getStringCellValue());
            employee.setEmail(row.getCell(3).getStringCellValue());
            Department department = new Department();
            department.setId(1L);
            employee.setDept(department);
            employee.setHireDate(new Date());
            employee.setState(true);
            employee.setAdmin(true);
            employee.setPassword("1");
            service.saveOrUpdate(employee);
        }
    }

    @RequestMapping("selectAll")
    @ResponseBody
    public List<Employee> selectAll() throws Exception {
        List<Employee> employees = service.selectAll();
        return employees;
    }

}
